package com.findfuture.demo.service.impl;

import com.findfuture.common.dao.HibernateDaoImpl;
import com.findfuture.demo.model.Catalog;
import com.findfuture.demo.service.ICatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Alex
 * Date: 12-2-5
 * Time: 下午2:55
 * To change this template use File | Settings | File Templates.
 */
@Service("catalogService")
public class CatalogService implements ICatalogService {

    @Autowired
    private HibernateDaoImpl commonDao;

    @Override
    public List<Catalog> getCatalogList() {
        String hql = "from " + Catalog.class.getSimpleName();
        commonDao.setEntryClass(Catalog.class);
        return commonDao.query(hql , null);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void addCatalog(String catalogName) throws Exception {
        commonDao.setEntryClass(Catalog.class);
        Catalog catalog = new Catalog();
        catalog.setName(catalogName);
        commonDao.save(catalog);
    }

    @Override
    public Catalog getCatalogById(int id) {
        commonDao.setEntryClass(Catalog.class);
        return (Catalog) commonDao.fetch(id);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void updateCatalog(Catalog catalog) throws Exception {
        commonDao.setEntryClass(Catalog.class);
        commonDao.update(catalog);
    }

    @Override
    public void deleteCatalog(int catalogId) throws Exception {
        commonDao.setEntryClass(Catalog.class);
        commonDao.execute("delete from " + Catalog.class.getSimpleName() + " where id = " + catalogId , null);
    }
}
